package com.sixkey.exception;

import com.sixkey.model.response.ResponseInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * ClassName: ExceptionHandlerAdvice
 * Package: com.sixkey.exception
 * Description:
 *
 * @Author: @WeiXueShi
 * @Create: 2023/8/15 - 21:02
 * @Version: v1.0
 */
@Slf4j
@RestControllerAdvice
public class ExceptionHandlerAdvice {

    /**
     * 捕获 {@code BusinessException} 异常
     */
    @ExceptionHandler({BusinessException.class})
    public ResponseInfo<?> handleBusinessException(BusinessException ex) {
        return ResponseInfo.fail(ex.getMessage());
    }

    /**
     * 参数校验异常捕获
     * @param e
     * @return
     */
    @ExceptionHandler({BindException.class})
    public ResponseInfo<?> handleException(BindException e) {
        return ResponseInfo.fail(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }
}
